<?php
/**
 * Created by PhpStorm.
 * User: mj
 * Date: 17-6-7
 * Time: 下午2:19
 */

namespace App\Winged\Auth\Api;

use App\Winged\Auth\Backend\Sign\Sign;

class User extends Api
{
    public static $actions
        = [
            'info'                   => 'uaa/user_info/user',
            'roles'                  => 'uaa/auth/roles',
            'permissions'            => 'uaa/auth/permissions',
            'user_roles_permissions' => 'uaa/auth/user_roles_permissions',
            'role_permissions'       => 'uaa/auth/role_permissions',
            'users'                  => 'uaa/user_info/get_doctor_list',
            'doctor_info'            => 'uaa/user_info/get_doctor_info',
            'update_user_info'       => 'uaa/user_info/update_user_info',
            'create'                 => 'uaa/auth/create_staff_user',
            'reset'                  => 'uaa/auth/change_pwd',
            'department'             => 'uaa/user_info/get_department_list',
        ];

    /**
     * 登录用户获取信息
     *
     * @return \Illuminate\Support\Collection|string
     */
    public static function info()
    {
        return self::doGet('info');
    }

    /**
     * 获取用户信息接口
     */
    public static function doctorInfo($id)
    {
        return self::doPost('doctor_info', ['data' => json_encode(['user_id' => user_id(), 'doctor_user_id' => $id])]);
    }

    /**
     * 更新用户信息
     */
    public static function updateUserInfo($data)
    {
        return self::doPost("update_user_info", ['data' => json_encode($data)]);
    }


    /**
     * 获取系统所有角色信息
     *
     * @return \Illuminate\Support\Collection|string
     */
    public static function roles()
    {
        return self::doGet('roles');
    }

    /**
     * 获取角色所有信息
     *
     * @return \Illuminate\Support\Collection|string
     */
    public static function permissions()
    {
        return self::doGet('permissions');
    }

    /**
     * 更新用户角色和权限
     *
     * @param $info = ['user_id' , 'roles' => [1,2,3] , 'permissions' => [1,2,3]];
     *
     * @return \Illuminate\Support\Collection|string
     */
    public static function updateUserRoles($info)
    {
        return self::doPut('user_roles_permissions', ['data' => json_encode($info)]);
    }

    /**
     * 更新角色权限
     *
     * @param $info = ['role_id' => 1 , 'permissions' => [1,2,3,4]] ;
     *
     * @return \Illuminate\Support\Collection|string
     */
    public static function updateRoleAndPermissions($info)
    {
        return self::doPost('role_permissions', $info);
    }

    public static function users($data)
    {
        return Api::formatResponse(self::doPost('users', $data));
    }

    public static function resigter($data)
    {
        $sign = Sign::getInstance(config('winged.backend.public_key'))->sign($data);

        return self::doPost('create', ['data' => $sign]);
    }

    public static function reset($data)
    {
        $sign = Sign::getInstance(config('winged.backend.public_key'))->sign($data);

        return self::doPost('reset', ['data' => $sign]);
    }

    public static function getDepartmentList($data)
    {
        return self::doPost('department', $data);
    }
}
